package com.dq.domain.sys.user;

import com.dq.domain.service.ServiceProject;
import com.dq.domain.subscribe.Subscribe;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.domain.sys.user.user.centre.UserCard;
import com.dq.utils.TimeUtil;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;

import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.base.SysUser;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import java.math.BigDecimal;


/**
 * @创建人 gewenzhen
 * @创建时间 2019/9/16
 * @描述
 */
@Data
@Entity
@ApiModel("充值记录")
public class Account extends BaseEntity implements VO.ToVO {
    @ApiModelProperty("会员卡")
    @ManyToOne
    protected UserCard userCard;
    @ApiModelProperty("用户")
    @ManyToOne
    protected WxUser user;

    @ApiModelProperty("组织部门")
    @ManyToOne
    protected SysSection sysDept;
    @ApiModelProperty("技师")
    @ManyToOne
    protected BossUser staffUser;
    @ApiModelProperty("账户变动类型")
    @Enumerated(EnumType.ORDINAL)
    protected AccountChangeType accountChangeType;

    @ApiModelProperty("变动类型")
    @Enumerated(EnumType.ORDINAL)
    protected ChangeType changeType;

    @ApiModelProperty("变动金额")
    @Column(columnDefinition="decimal default 0")
    protected BigDecimal price;

    @ApiModelProperty("赠送金额")
    @Column(columnDefinition="decimal default 0")
    protected BigDecimal givePrice;

    @ApiModelProperty("剩余正常金额")
    @Column(columnDefinition="decimal default 0")
    protected BigDecimal surplusPrice;
    @ApiModelProperty("剩余赠送金额")
    @Column(columnDefinition="decimal default 0")
    protected BigDecimal surplusGivePrice;

    @ApiModelProperty("支付方式")
    @Enumerated(EnumType.ORDINAL)
    protected AccountPayType accountPayType;


    @ApiModelProperty("充值方式")
    @Enumerated(EnumType.ORDINAL)
    protected RechargeType rechargeType;

    @ApiModelProperty("记录状态")
    @Enumerated(EnumType.ORDINAL)
    protected Status status;

    @ApiModelProperty("备注")
    public String remarks;

    @ManyToOne
    @ApiModelProperty("预约单")
    private Subscribe subscribe;

    @ManyToOne
    @ApiModelProperty("服务项目")
    private ServiceProject serviceProject;

    @ApiModelProperty("订单号")
    private String orderNo;
    @ApiModelProperty("支付状态")
    @Enumerated(EnumType.ORDINAL)
    private PayStatus payStatus;
    @ApiModelProperty("qrOssUrl")
    private String qrOssUrl;
    @ApiModelProperty("支付时间")
    private Long payTime;
    @ApiModelProperty("sign")
    @Column(columnDefinition = "text")
    private String sign;

    public enum AccountPayType {
        alipay("支付宝"),
        wxpay("微信"),
        card("刷卡支付"),
        cash("现金支付"),
        online("在线支付"),
        vipCard("会员卡积分"),
        vipCardProject("会员卡项目次数结账")
        ;

        public String name;

        AccountPayType(String name) {
            this.name = name;
        }
    }

    public enum ChangeType {
        add("加"),
        less("减"),;
        public String mean;

        ChangeType(String mean) {
            this.mean = mean;
        }
    }

    public enum AccountChangeType {
       recharge("充值"),deduct("消耗");

        AccountChangeType(String name) {
            this.name = name;
        }
        public String name;
    }

    public enum RechargeType{
        staff("员工带单"),
        teacher("老师带单"),
        guide("导购带单")
        ;
        public String name;
        RechargeType(String name) {
            this.name = name;
        }
    }

    public enum Status{
        normal("正常"),
        abnormal("异常")
        ;
        public String name;
        Status(String name) {
            this.name = name;
        }
    }

    public enum PayStatus{
        success("成功"),
        erro("失败")
        ;
        public String name;
        PayStatus(String name) {
            this.name = name;
        }
    }

    @Data
    @ApiModel(value = "账户视图")
    public static class Vo implements VO {
        @ApiModelProperty("id")
        public Long id;
        @ApiModelProperty("所属顾客")
        public String wxUserRealcName;
        @ApiModelProperty("充值会员卡")
        public String cardNo;
        @ApiModelProperty("会员卡类型")
        protected String userCartTypeCN;
        @ApiModelProperty("充值类型")
        protected String rechargeTypeCN;
        @ApiModelProperty("变动金额")
        protected BigDecimal price;
        @ApiModelProperty("正常剩余")
        protected BigDecimal surplusPrice;
        @ApiModelProperty("赠送剩余")
        protected BigDecimal surplusGivePrice;
        @ApiModelProperty("服务店面")
        protected String orgName;
        @ApiModelProperty("服务人员")
        protected String waiterPeople;
        @ApiModelProperty("总消费金额")
        protected String payTotal;
        @ApiModelProperty("充值时间")
        protected String createTime;
        @ApiModelProperty("资金变动类型")
        protected String changeType;
        @ApiModelProperty("备注")
        public String remarks;
        @ApiModelProperty("qrOssUrl")
        private String qrOssUrl;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id=this.getId();
        vo.wxUserRealcName = this.getUser() != null ? this.getUser().getUserRealcName() : "";
        vo.cardNo = this.getUserCard() != null ? this.getUserCard().getCardNo() : "";
        vo.userCartTypeCN = this.getUserCard() != null ? this.getUserCard().getCardType().name : "";
        vo.rechargeTypeCN = this.getRechargeType().name;
        vo.price = this.getPrice();
        vo.surplusPrice = this.getSurplusPrice();
        vo.surplusGivePrice = this.getSurplusGivePrice();
        vo.orgName = this.getSysDept().getSimpleName();
        vo.waiterPeople=this.getStaffUser() != null ? this.getStaffUser().getUserRealcName():"";
        if(this.getUser().getPayTotal()!=null && this.getUser().getPayTotal()!=null){
            vo.payTotal=this.getUser().getPayTotal().toPlainString();
        }else{
            vo.payTotal="0.00";
        }
        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.changeType=this.getChangeType().name();
        vo.remarks = fmt(remarks);
        vo.qrOssUrl = fmt(this.getQrOssUrl());

        return vo;
    }

    @Data
    public static class FrontAccount{
        public Long id;
        public Long userCardId;
        public String cardNo;
        public Long wxUserId;
        public String wxUserRealcName;
        public Long staffUserId;
        public String staffUserRealcName;
        @ApiModelProperty("账户变动类型")
        protected String accountChangeTypeEN;
        protected String accountChangeTypeCN;
        @ApiModelProperty("变动类型")
        protected String changeTypeEN;
        protected String changeTypeCN;
        @ApiModelProperty("变动金额")
        protected BigDecimal price;
        @ApiModelProperty("赠送金额")
        protected BigDecimal givePrice;
        @ApiModelProperty("支付方式")
        protected String accountPayTypeEN;
        protected String accountPayTypeCN;
        @ApiModelProperty("充值方式")
        protected String rechargeTypeEN;
        protected String rechargeTypeCN;


        @ApiModelProperty("卡类型")
        protected String userCartTypeCN;
        protected String userCartTypeEN;
        @ApiModelProperty("剩余正常金额")
        protected BigDecimal surplusPrice;
        @ApiModelProperty("剩余赠送金额")
        protected BigDecimal surplusGivePrice;
        @ApiModelProperty("服务店面")
        protected String orgName;
        @ApiModelProperty("服务人数")
        protected Long waiterNum;
        @ApiModelProperty("服务人员")
        protected String waiterPeople;
        @ApiModelProperty("我的业绩")
        protected BigDecimal myAchievements;

        protected String createTime;
        @ApiModelProperty("备注")
        public String remarks;

        @ApiModelProperty("总消费金额")
        protected String payTotal;
    }

    public FrontAccount toFrontAccount(){
        FrontAccount vo = new FrontAccount();
        vo.id = this.getId();
        vo.userCardId = this.getUserCard() != null ? this.getUserCard().getId() : null;
        vo.cardNo = this.getUserCard() != null ? this.getUserCard().getCardNo() : "";
        vo.wxUserId = this.getUser() != null ? this.getUser().getId() : null;
        vo.wxUserRealcName = this.getUser() != null ? this.getUser().getUserRealcName() : "";
        vo.staffUserId = this.getStaffUser() != null ? this.getStaffUser().getId() : null;
        vo.staffUserRealcName = this.getStaffUser() != null ? this.getStaffUser().getUserRealcName() : "";
        vo.accountChangeTypeEN = this.getAccountChangeType() == null ? "" : this.getAccountChangeType().toString();
        vo.accountChangeTypeCN = this.getAccountChangeType() == null ? "" : this.getAccountChangeType().name;
        vo.changeTypeEN = this.getChangeType().toString();
        vo.changeTypeCN = this.getChangeType().mean;
        vo.price = this.getPrice();
        vo.givePrice = this.getGivePrice();
        vo.accountPayTypeEN = this.getAccountPayType() == null ? "" : this.getAccountPayType().toString();
        vo.accountPayTypeCN = this.getAccountPayType() == null ? "" : this.getAccountPayType().name;
        vo.rechargeTypeEN =this.getRechargeType() != null? this.getRechargeType().toString():"";
        vo.rechargeTypeCN =this.getRechargeType() != null?  this.getRechargeType().name:"";

        vo.userCartTypeCN = this.getUserCard() != null ? this.getUserCard().getCardType().name : "";
        vo.userCartTypeEN = this.getUserCard() != null ? this.getUserCard().getCardType().toString() : "";
        vo.surplusPrice = this.getSurplusPrice();
        vo.surplusGivePrice = this.getSurplusGivePrice();
        vo.orgName = this.getSysDept().getSimpleName();
        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.remarks = fmt(remarks);

        if(this.getUser().getPayTotal()!=null && this.getUser().getPayTotal()!=null){
            vo.payTotal=this.getUser().getPayTotal().toPlainString();
        }else{
            vo.payTotal="0.00";
        }
        return vo;
    }
}
